$(document).ready(function() {
    query();
});

/*增加课程*/
$(document).on('click', '#addCourse-btn', function() {
    var $courseName=$("input[name='courseName']").val();
    var $courseNum=$("input[name='courseNum']").val();
    var $academyName=$("input[name='academyName']").val();
    var $courseCredihour=$("input[name='courseCredihour']").val();
    var $coursePeriod=$("input[name='coursePeriod']").val();
    var $courseType=$("input[name='courseType']").val();
    alert($courseName+$courseNum+$academyName+$courseCredihour+$coursePeriod+$courseType);
    $.ajax({
        url:"http://localhost:8080/course/addCourse",
        type:"GET",
        dataType:"json",
        data:"courseName="+$courseName+"&courseNum="+$courseNum+"&academyName="+$academyName+"&courseCredihour="+$courseCredihour+"&coursePeriod="+$coursePeriod+"&courseType="+$courseType,
        success:function (data) {
            console.log(data.data)
            alert("添加课程成功！")
        },
        error:function (data) {
            alert("添加课程失败！。")
        }
    })
});


/*查询全部课程*/
var query = function () {
    $('#dataTable').dataTable({
        "bDeferRender": true,
        "sAjaxDataProp": "data.courseAcademy",
        "sAjaxSource": "http://localhost:8080/course/queryAllCourse",
        "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
        "fnPreDrawCallback": function (oSettings) {
            $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
            $('.dataTables_length select').addClass('form-control input-small');
        },
        "oLanguage": {
            "sSearch": "",
            "sLengthMenu": "<span>_MENU_ entries</span>"
        },
        "bJQueryUI": false,
        "bAutoWidth": false,
        "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",
        "aoColumnDefs": [
            {
                "mDataProp": null,
                "sDefaultContent": "<a href='#'><span  class='label label-danger mr10 mb10' id='delete'>删除</span></a>" +
                "<a href='#'><span  class='label label-yellow mr10 mb10' id='update'>修改</span></a>",
                "aTargets": [-1]
            }
        ],
        "aoColumns": [
            {"mDataProp": "courseName"},
            {"mDataProp": "courseNum"},
            {"mDataProp": "academyName"},
            {"mDataProp": "courseCredithour"},
            {"mDataProp": "coursePeriod"},
            {"mDataProp": "courseType"},
            {"mDataProp": ""},
        ],
    })
};

/*删除课程*/
$(document).on('click', '#delete', function () {
    //获取课程名称$courseName
    var $courseName = $(this).parents("tr").children(0).eq(0).text();
    alert($courseName)
    $.ajax({
        url: "http://localhost:8080/course/deleteCourse",
        type: "GET",
        async: true,
        data: "courseName=" + $courseName,
        dataType: "json",
        success: function () {
            location.reload();
            layer.alert("操作成功!");
        },
        error: function () {
            layer.alert("操作失败!");
        }
    });
});

/* 更新代码*/

$(document).on('click', '#update', function () {
    //获取$noticeId
    var $courseNameBefore = $(this).parents("tr").children(0).eq(0).text();
    //页面层
    layer.open({
        type: 2,
        title: '更新课程',
        area: ['1500px', '400px'],
        content: ['courseUpdata.html?courseNameBefore='+$courseNameBefore, 'no']
    })
})